package com.hkyl.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.hkyl.hospital.entity.*;
import com.hkyl.hospital.mapper.DeptMapper;
import com.hkyl.hospital.mapper.SickroomMapper;
import com.hkyl.hospital.mapper.UserMapper;
import com.hkyl.hospital.mapper.UserRoleMapper;
import com.hkyl.hospital.mapper.custom.CustomHospitalizedMapper;
import com.hkyl.hospital.mapper.custom.CustomPatientMapper;
import com.hkyl.hospital.service.DeptService;
import com.hkyl.hospital.service.HospitalizedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sjh
 * @date 2023/07/13 16:27:12
 */
@Service
@Transactional
public class HospitalizedServiceImpl implements HospitalizedService {


    Logger logger = LoggerFactory.getLogger(HospitalizedServiceImpl.class);

    @Autowired
    private CustomHospitalizedMapper hospitalizedMapper;
    @Autowired
    private CustomPatientMapper patientMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private SickroomMapper sickroomMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;




    @Autowired
    private SickroomMapper sickroomMapper;

    @Override
    public int insert(Hospitalized hospitalized) {
        return hospitalizedMapper.insert(hospitalized);
    }

    @Override
    public int delete(Integer hospitalizedId) {
        hospitalizedMapper.goOutHospital(hospitalizedId);
        return hospitalizedMapper.fakedelete(hospitalizedId);
    }

    @Override
    public int update(Hospitalized hospitalized) {
        return hospitalizedMapper.updateByPrimaryKeySelective(hospitalized);
    }

    @Override
    public List<Hospitalized> selectAll(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Hospitalized> hospitalizeds = hospitalizedMapper.selectByExample(null);

        return getHospitalizeds(hospitalizeds);
    }
    @Override
    public List<Hospitalized> selectIn(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return getHospitalizeds(hospitalizedMapper.selectIn());
    }

    @Override
    public List<Hospitalized> selectOut(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return getHospitalizeds(hospitalizedMapper.selectOut());
    }

    @Override
    public List<Hospitalized> selectByName(String patientName, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        PatientExample patientExample = new PatientExample();
        patientExample.or().andPatientNameLike("%" + patientName + "%");
        List<Patient> patients = patientMapper.selectByExample(patientExample);
        List<Hospitalized> list = new ArrayList<>();

        List<Hospitalized> hospitalizeds = hospitalizedMapper.selectByExample(null);
        for (Patient patient: patients ) {
            List<Hospitalized> collect = hospitalizeds.stream().filter(hospitalized -> hospitalized.getPatientId() == patient.getPatientId()).collect(Collectors.toList());
            list.addAll(collect);
        }
        return getHospitalizeds(list);
    }

    @Override
    public Hospitalized findByPatientId(Integer patientId) {
        HospitalizedExample hospitalizedExample = new HospitalizedExample();
        hospitalizedExample.or().andPatientIdEqualTo(patientId).andStatusEqualTo(0);
        List<Hospitalized> hospitalizeds = hospitalizedMapper.selectByExample(hospitalizedExample);
        if (hospitalizeds.size() > 0) {
            getHospitalizeds(hospitalizeds);
            setDeptAndDoctorAndSickrooms(hospitalizeds.get(0));
            return hospitalizeds.get(0);
        }
        return null;
    }

    @Override
    public Hospitalized updateReaded(Hospitalized hospitalized) {
       int rows =  hospitalizedMapper.updateByPrimaryKeySelective(hospitalized);
        if (!(rows > 0)) {
            throw new RuntimeException("出错了");
        }
        return hospitalized;
    }

    private void setDeptAndDoctorAndSickrooms(Hospitalized hospitalized) {
        Patient patient = patientMapper.selectByPrimaryKey(hospitalized.getPatientId());
        hospitalized.setPatient(patient);
        Dept dept = deptMapper.selectByPrimaryKey(hospitalized.getDeptId());
        hospitalized.setDept(dept);
        hospitalized.setSickroom(sickroomMapper.selectByPrimaryKey(hospitalized.getSickroomId()));
        hospitalized.setUser(userMapper.selectByPrimaryKey(hospitalized.getDoctor()));
    }


    private List<Hospitalized> getHospitalizeds(List<Hospitalized> hospitalizeds) {
        List<Patient> patients = patientMapper.selectByExample(null);
        List<User> userList = new ArrayList<>();
        for (Patient patienr : patients) {
            hospitalizeds.stream().filter(hospitalized -> hospitalized.getPatientId() == patienr.getPatientId()).forEach(hospitalized -> hospitalized.setPatient(patienr));
        }

        List<Dept> depts = deptMapper.selectByExample(null);
        for (Dept dept : depts) {
            hospitalizeds.stream().filter(hospitalized -> hospitalized.getDeptId() == dept.getDeptId()).forEach(hospitalized -> hospitalized.setDept(dept));
        }

        List<User> users = userMapper.selectByExample(null);
        for (User user : users ) {
            hospitalizeds.stream().filter(hospitalized -> hospitalized.getDoctor() == user.getUserId()).forEach(hospitalized -> hospitalized.setUser(user));
        }

        List<Sickroom> sickrooms1 = sickroomMapper.selectByExample(null);
        for (Sickroom sickroom : sickrooms1) {
            hospitalizeds.stream().filter(hospitalized -> hospitalized.getSickroomId() == sickroom.getSickroomId()).forEach(hospitalized -> hospitalized.setSickroom(sickroom));
        }

        List<Sickroom> sickrooms = sickroomMapper.selectByExample(null);
        List<Dept> depts1 = deptMapper.selectByExample(null);
        List<Dept> collect = depts1.stream().filter(dept -> dept.getDeptName().equals("医务科")).collect(Collectors.toList());
        hospitalizeds.stream().forEach(hospitalized -> hospitalized.setSickrooms(sickrooms));
        hospitalizeds.stream().forEach(hospitalized -> hospitalized.setDepts(collect));
       /* List<User> users1 = hospitalizedMapper.selectDoctor();
        logger.info(users1.toString());
        hospitalizeds.stream().forEach(hospitalized -> hospitalized.setUsers(users1));*/
        List<UserRoleKey> userRoleKeys = userRoleMapper.selectByExample(null);
        List<UserRoleKey> collect1 = userRoleKeys.stream().filter(userRoleKey -> userRoleKey.getRoleId() == 4).collect(Collectors.toList());
        for (UserRoleKey userrole: userRoleKeys) {
            List<User> collect2 = users.stream().filter(user -> user.getUserId() == userrole.getUserId()).collect(Collectors.toList());
            userList.addAll(collect2);
        }
        hospitalizeds.stream().forEach(hospitalized -> hospitalized.setUsers(userList));
        return hospitalizeds;
    }


}
